from StringIO import StringIO
from Products.CMFCore.utils import getToolByName
from Products.LMSkin.content.vocabularies import *
from Products.LMSkin.config import *
from Products.LMSkin.AppConfig import *
#from Products.LMSkin.Extensions.install_taxonomy import *
from Products.ExternalMethod.ExternalMethod import ExternalMethod
from Products.CMFPlone.migrations.migration_util import safeEditProperty

INDEX_HTML = '''
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      lang="en"
      metal:use-macro="here/main_template/macros/master"
      i18n:domain="plone">

<head>
    <metal:block fill-slot="top_slot"
                 tal:define="dummy python:request.set('disable_border',1)" />
</head>

<body>
<metal:main fill-slot="main">
    <table width="100%" border="0">
        <tr>
            <td valign="top" width="65%">
                <div metal:use-macro="here/portlet_news/macros/portlet">
                    NEWS
                </div>
                
                <div id="segunda_principal">
                    <div metal:use-macro="here/portlet_artigos/macros/portlet">
                        portlet com os ultimos artigos publicados
                    </div>
                    
                    <div metal:use-macro="here/portlet_dicas/macros/portlet">
                        portlet com dicas
                    </div>              
                </div>

                <div id="terceira_principal">
                    <div metal:use-macro="here/portlet_desen_agil/macros/portlet">
                        portlet com os ultimos itens da area de desenvolvimento ágil
                    </div>
                    
                    <div metal:use-macro="here/portlet_eventos/macros/portlet">
                        portlet com os ultimos eventos
                    </div>              
                </div>
                <div id="quarta_principal">
                    <div metal:use-macro="here/portlet_novidades/macros/portlet">
                        ultimas aterações
                    </div>
                    
                    <div metal:use-macro="here/portlet_links/macros/portlet">
                        portlet com os ultimos eventos
                    </div>              
                </div>
            </td>
            
            <td valign="top" >
                <div metal:use-macro="here/portlet_profissionais/macros/portlet">
                    portlet com informacoes profissional
                </div>
            </td>
        </tr>
    </table>   
</metal:main>
</body>
</html>
'''

def replaceIndex(portal, out):
    print >> out, "Installation index_html"  
 
    if 'front-page' in portal.objectIds():
       portal.manage_delObjects('front-page')

    if 'index_html' in portal.objectIds():
       portal.manage_delObjects('index_html')

    portal.manage_addProduct['PageTemplates'].manage_addPageTemplate('index_html', '', INDEX_HTML)

def setupPortal(portal, out):
    print >> out, "Installation properties"
    portal.manage_changeProperties(title='LM - Software Livre e Desenvolvimento Ágil',
                                   email_from_address='leonardo.mirandas@gmail.com',
                                   email_from_name= 'Administrador')



def setupSkin(portal, out):
    print >> out, "Installation skin"
    portal_skins = getToolByName(portal, 'portal_skins')
    path = portal_skins.getSkinPath('Plone Default')
    path = [elem.strip() for elem in path.split(',') if elem != '.svn']
    path = ','.join(path)
    portal_skins.addSkinSelection('Portal LM', path)
    portal_skins.manage_properties(default_skin = 'Portal LM',
			           request_varname = 'plone_skin',
			           del_skin = 0, 
                                   chosen = [])

def addCSS(portal, out):
    print >> out, "Installation style"
    portal.portal_css.manage_addStylesheet(id='LMSkinCustom.css',
                                               expression='',
                                               media='screen',
                                               rel='sylesheet',
                                               title='',
                                               rendering='import',
                                               enabled=True,
                                               cookable=True,
                                               REQUEST=None,)


def setupPortlet(portal, out):
    print >> out, "Installation portlets"
    #add portlet in left_slots
    left_slots=('here/portlet_login/macros/portlet'
                ,'here/portlet_user/macros/portlet')

    #add portlet in right_slots
    right_slots=()

    safeEditProperty(portal, 'left_slots', left_slots)
    safeEditProperty(portal, 'right_slots', right_slots)

def setupTypes(portal, out):
    print >> out, "Installation structure"

    portal_types = getToolByName(portal, 'portal_types')
    addable_types=['Document','Image','Folder','File','Dica','Artigo','Curriculo','News Item']

    portal_types['Plone Site'].filter_content_types = True
    portal_types['Plone Site'].allowed_content_types = []

    portal_types['ATBTreeFolder'].filter_content_types = True
    portal_types['ATBTreeFolder'].allowed_content_types = addable_types

def setupMenu(portal, out):
    print >> out, "Installation menu"
       
    #dicionario de menus
    menu_publico = ['artigos:Artigos','software_livre:Software Livre','noticias:Notícias','eventos:Eventos','desenvolvimento:Desenvolvimento Ágil','curriculos:Banco de Currículos','colaboradores:Colaboradores','listas_discurssoes:Listas de discurssões','zope:Zope','plone:Plone','django:Django','python:Python','novidades:Últimas Atualizações',]

    #create menu
    for m in menu_publico:
        m = m.split(':')
        id_menu = m[0]
        titulo_menu = m[1]
        propiedade = 'sessao_'+m[0]+'_index'
        style = 'sessao_'+m[0]
        if id_menu not in portal.objectIds():
            portal.invokeFactory('Folder', id=id_menu, title=titulo_menu)
            item = getattr(portal, id_menu)
            item.manage_addProperty('default_page', propiedade, 'string')
            item.manage_addProperty('style', style, 'string')


def install(self):
    out = StringIO()
    portal = self
    replaceIndex(portal, out)
    setupPortal(portal, out)
    setupSkin(portal, out)
    addCSS(portal, out)
    setupPortlet(portal, out)
    setupMenu(portal, out)
    #setLMProperties(portal, out)

def uninstall(self):
    portal = self
    portal_types = getToolByName(portal, 'portal_types')
    portal_types['Plone Site'].global_allow = True
    portal_types['Plone Site'].filter_content_types = False
